<template>
    <div ref="chartBarRef">
        <el-card>
            <div class="flex flex-jc-sb">
                <div class="fs-14 bold">统计分析</div>
                <div class="flex">
                    <el-select v-model="sltVal" size="small" @change="handleChange">
                        <el-option v-for="(item, index) in options" :key="item.value" :label="item.label"
                            :value="item.value"></el-option>
                    </el-select>
                    <customer-switch v-model:active="active" class="mg-l-20" :list="list"
                        @change="handleSwitchChange"></customer-switch>
                </div>
            </div>
            <div class="chart-container" ref="chartRef">
            </div>
        </el-card>
    </div>
</template>
<script setup>
import { ref, computed, watch, onMounted,onActivated } from 'vue'
import * as echarts from 'echarts'


const props = defineProps({
    title: {
        type: String,
        default: '近七日销售趋势'
    },
    days: {
        type: Number
    },
    chartData: {
        type: Object
    }
})

const emits = defineEmits(['update:days', 'update'])

const active = ref(7)
const list = ref([
    {
        name: '本周',
        value: 7
    },
    {
        name: '本月',
        value: 30
    },
])

const sltVal = ref(1)
const options = ref([
    {
        label: '领薪人数',
        value: 1,
    },
    {
        label: '发薪金额',
        value: 2,
    },
    {
        label: '员工库',
        value: 3,
    }
])

const seriesName = () => {
    const cur = options.value.find(i => i.value === sltVal.value)
    return cur.label
}

const handleSwitchChange = () => {
    emits('update:days', active.value)
    emits('update', active.value)
}

const chartRef = ref(null)
const chartBarRef = ref(null)
let myChart = null

let date = []
let data = []

const createChartOption = (date, data) => {
    const chartOption =
    {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: date,
            axisLine: {
                show: true,
                lineStyle: {
                    color: '#EAEAEF',
                    type: 'solid'
                },
            },
            axisLabel: {
                show: true,
                color: '#9497B1',
                margin: 20
            }
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                lineStyle: {
                    type: 'dashed'
                }
            },
            axisLabel: {
                show: true,
                color: '#9497B1'
            }
        },
        grid: {
            left: 40,
            right: 40,
            top: 30,
            bottom: 30,
        },
        series: [
            {
                name: seriesName(),
                type: 'line',
                showSymbol: false,
                sampling: 'lttb',
                smooth: true,
                itemStyle: {
                    color: 'rgba(12, 83, 252, 1)'
                },
                areaStyle: {
                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                        {
                            offset: 0,
                            color: 'rgba(12, 83, 252, 0.2)'
                        },
                        {
                            offset: 1,
                            color: 'rgba(12, 83, 252, 0)'
                        }
                    ])
                },
                data: data
            }
        ]
    }
    return chartOption
}

const handleChange = () => {
    setOption()
}

const setOption = () => {
    const map = {
        1: 'receivePeople',
        2: 'salaryAmount',
        3: 'employees'
    }
    date = props.chartData.date
    data = props.chartData[map[sltVal.value]]
    myChart.setOption(createChartOption(date, data))
}

const resize = () => {
    myChart.resize()
}

watch(() => props.days, (newval) => {
    if (newval) {
        active.value = newval
    }
}, {
    immediate: true
})


watch(() => props.chartData, (newval) => {
    if (newval && Object.keys(newval)) {
        setOption()
    }
}, {
    deep: true
})

defineExpose({
    chartBarRef,
    resize
})

onMounted(() => {
    myChart = echarts.init(chartRef.value)
    console.log(1111, myChart)
})
onActivated(() => {
    myChart = echarts.init(chartRef.value)
    console.log(1111, myChart)
})
</script>
<style lang="scss" scoped>
.chart-container {
    width: 100%;
    height: 290px;
}
</style>